package Command;

import Server.Command;
import Server.cmdArguments;

public class FibbCommand implements Command {
	private Integer i;
	private cmdArguments args;

	public FibbCommand() {}

	public Integer fib(Integer n) {
		if (n < 40) {
			if (n <= 1) return n;
			else return fib(n-1) + fib(n-2); 
		}
		return new Integer(0);
	}

	@Override
	public String execute() {
		String result = "";
		try {
			i = Integer.parseInt(args.commands[1].trim());
			i = (int)fib(i);
			if(i>0){
				args.bst.insert(i);
				if(args.db){
					result = "Inserted: " + i;
				} else {
					result = "Inserter " + i + " to the dataset, commit to make insert final.";
				}
			} else{
				return "My fibonacci calculator is very inefficient and can not calculate fibonacci >39th.";
			}

		} catch (Exception e) {
			result = "Missing or incorrect argument";
		}
		return result;
	}

	@Override
	public String undo() {
		if (args.bst.delete(i))
			return "Undid adding: " + i + ".";
		return "Undo failed.";
	}

	@Override
	public void setArgs(cmdArguments args) {
		this.args = args;

	}

}
